#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void InitSeqList(SL* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}


void SeqListCheakCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SQDateType* tmp = (SQDateType*)realloc(ps->a, newcapacity * sizeof(SQDateType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
	}		
}


void SeqListPrint(SL* ps)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SeqListDestory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}


void SeqlistPushBack(SL* ps, SQDateType x)
{
	SeqListInsert(ps, ps->size, x);
}


void SeqlistPushFront(SL* ps, SQDateType x)
{
	SeqListInsert(ps, 0, x);
}


void SeqlistPopBack(SL* ps)
{
	SeqListErase(ps, ps->size - 1);
}


void SeqlistPopFront(SL* ps)
{
	SeqListErase(ps, 0);
}


void SeqListInsert(SL* ps, int pop, SQDateType x)
{
	SeqListCheakCapacity(ps);
	int end = ps->size;
	while (end > pop)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pop] = x;
	ps->size++;
}


void SeqListErase(SL* ps, int pop)
{
	while (pop < ps->size)
	{
		ps->a[pop] = ps->a[pop + 1];
		pop++;
	}
	ps->size--;
}
